#include "functions.h"






// analyse une chaine de caratère et la trie

commandLine analyseCommandLine(char * line){
	int i=0,j,c,counter,p;
	char *temp2 = (char *) malloc(sizeof(char)*MAXLINE);
	commandLine commandLine = NULL;
	commandLine = (listOfCommands *) malloc(sizeof (listOfCommands));
	commandLine = commandLineInitialisation(commandLine);
	listOfCommands* commandTemp = NULL;
	commandTemp = commandLine;
	char *astuce = (char *) malloc(strlen(line) + 1 + sizeof("    "));
	astuce = strcpy(astuce , "  ");
	astuce = strcat(astuce, line);
	line = astuce;
	line = strcat(line, " "); 
	
	while (line[i]!='\0'){
		if (line[i] == ' '){
			j=i;
			while (line[j] == ' ' && line[j] != '\0'){
				j++;
			}
			c = j;
			while (line[c] != ' ' && line[c] != '\0') {
				c++;
			}
			
			if (j != i+1 && line[j] != '\0' && line[c] != '\0' ){
				p=0;
				for (counter = j; counter < c; counter++){
				temp2[p] = line[counter];
				p++;
				} 
				if (strcmp (temp2,"|") != 0 && strcmp (temp2,">") !=0 && strcmp (temp2,"&") !=0){
					commandTemp = addArg(commandTemp, temp2);
					//printf("%s \n",temp2);				
					memset(temp2,0,MAXLINE);
				}
				if (strcmp (temp2,"|") != 0 && strcmp (temp2,">") !=0 && strcmp (temp2,"&") ==0){
					commandTemp->bg = TRUE;
					//printf("%s ",temp2);
					memset(temp2,0,MAXLINE);
				}
				if (strcmp (temp2,"|") != 0 && strcmp (temp2,">") ==0 && strcmp (temp2,"&") !=0){
					commandTemp->redirect = TRUE;
					//printf("%s \n",temp2);		
					memset(temp2,0,MAXLINE);
				}
				if (strcmp (temp2,"|") == 0 && strcmp (temp2,">") !=0 && strcmp (temp2,"&") !=0){
					commandTemp->pipe = TRUE;
					listOfCommands* newElement = (listOfCommands *) malloc(sizeof (listOfCommands));
					newElement = commandLineInitialisation(newElement);
					commandTemp->nxt = newElement;
					commandTemp = newElement;
					//printf("%s ",temp2);
					memset(temp2,0,MAXLINE);
				}
			}
			else if (line[j] != '\0' && line[c] != '\0'){
				p=0;
				for (counter = j ; counter < c; counter++){
				temp2[p] = line[counter];
				p++;
				} 
				if (strcmp (temp2,"|") != 0 && strcmp (temp2,">") !=0 && strcmp (temp2,"&") !=0){
					commandTemp = addArg(commandTemp, temp2);
					//printf("%s \n",temp2);		
					memset(temp2,0,MAXLINE);
				}
				if (strcmp (temp2,"|") != 0 && strcmp (temp2,">") !=0 && strcmp (temp2,"&") ==0){
					commandTemp->bg = TRUE;
					//printf("%s \n",temp2);
					memset(temp2,0,MAXLINE);
				}
				if (strcmp (temp2,"|") != 0 && strcmp (temp2,">") ==0 && strcmp (temp2,"&") !=0){
					commandTemp->redirect = TRUE;
					//printf("%s \n",temp2);		
					memset(temp2,0,MAXLINE);
				}
				if (strcmp (temp2,"|") == 0 && strcmp (temp2,">") !=0 && strcmp (temp2,"&") !=0){
					commandTemp->pipe = TRUE;
					listOfCommands* newElement = (listOfCommands *) malloc(sizeof (listOfCommands));
					newElement = commandLineInitialisation(newElement);
					commandTemp->nxt = newElement;
					commandTemp = newElement;
					//printf("%s \n",temp2);
					memset(temp2,0,MAXLINE);
				}
			}
			i = c-1 ;
		}
	i++;
	}
	free(line);
	free(temp2);
	
	return (commandLine);
}
// initialise un élément listOfCommands

listOfCommands* commandLineInitialisation(listOfCommands * listOfCommands){
	
	listOfCommands->arg = (char **)malloc( sizeof(char*));
	listOfCommands->arg[0] = NULL;
	listOfCommands->numArg = 1;
	listOfCommands->bg = FALSE;
	listOfCommands->pipe = FALSE;
	listOfCommands->redirect = FALSE;
	listOfCommands->nxt = NULL;
	return(listOfCommands);

}

// ajoute un arg, au listOfCommands->arg
listOfCommands* addArg(listOfCommands *listOfCommands, char *newArg){
	char **temp= NULL ;
	//printf("arg number : %d \n", listOfCommands->numArg);
	temp = (char **) malloc( (listOfCommands->numArg + 1) * sizeof(char *));
	
	int i= 0;
	for (i = 0; i< listOfCommands->numArg -1 ; i++){
		//printf("%s \n", listOfCommands->arg[i]);
		temp[i] = listOfCommands->arg[i];
		//printf("test1 \n");
		
		
		
	}
	free(listOfCommands->arg);
	

	
	temp[listOfCommands->numArg- 1] = (char *)malloc(strlen(newArg) +1 );
	strcpy(temp[listOfCommands->numArg - 1], newArg );
	temp[listOfCommands->numArg] = NULL;
	listOfCommands->arg = temp;
	listOfCommands->numArg++;
		
	return(listOfCommands);
}


// affiche un élément de la liste
void printListOfCommands(listOfCommands* commandLine){
	int i;
	printf("arg : ");
	for (i = 0 ; i< commandLine->numArg; i++){
		printf("%s,",commandLine->arg[i]);
	} 
	printf("\n");		
	printf("num arg : %d \n", commandLine->numArg);
	printf("bg : %d \n", commandLine->bg);
	printf("pipe : %d \n", commandLine->pipe);
	printf("redirect : %d \n", commandLine->redirect);
	printf("--------------------------------\n");


}

// permet d'effacer un élément de la liste et renvoie le pointeur vers l'élément suivant!
listOfCommands* deleteElement(listOfCommands* commandLine){
	int i;
	listOfCommands* tmp = NULL;
	for (i = 0 ; i< commandLine->numArg; i++){
		free(commandLine->arg[i]);
	} 
	free(commandLine->arg);
	tmp  = commandLine->nxt;
	free(commandLine);
	return (tmp);


}








